<?php

namespace app\admin\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use app\common\library\Id as L_ID;
use think\cache\driver\Redis;

class Id extends Command
{

    protected function configure()
    {
        //type:1是5位数，2是7位数,3是9位数
        $this->setName('Id')
            ->addOption('type', 'type', Option::VALUE_REQUIRED, '数字类型', 2)
            ->setDescription('生成唯一全局唯一ID');
    }

    protected function execute(Input $input, Output $output)
    {

        $type = $input->getOption('type') ?: 2;

        echo $type.PHP_EOL;
       
        $size=5000;//每次处理的个数
        $start=10000;
        $max=19999;

        if($type==1){
            $start=10000;
            $max=19999;
        }
        else if($type==2){
            $start=8000000;
            $max=8999999;
        }else if($type==3){
            $start=800000000;
            $max=899999999;
        }

        $num=$size;
       
        while($start<=$max){

            if($start+$size>=$max){
                $num=$max-$start+1;
            }

            echo "start:".$start.",num:".$num.PHP_EOL;

            $Id_list= $this->createId($num,$type,$start);
            if($type==3){
                db("unique_id_for_user")->insertAll( $Id_list);
            }else{
                db("unique_id")->insertAll( $Id_list);
            }

            $start+=$num;
        }
        echo "finished".PHP_EOL;
      
    }

    protected function createId($num,$type,$start){

        $len = 5;
        if ($type == 2) {
            $len = 7;
        }
        if ($type == 3) {
            $len = 9;
        }

        $Id_list=[];
        for ($i = 0; $i < $num; $i++) {

            $Id_list[] = [
                "unique_id" => L_ID::generateUniqueId($len - 1, $start),
                "createtime" => time(),
                "updatetime" => 0,
                "type" => $type,
            ];

            $start++;
        }

        return $Id_list;
    }
}
